﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using JohnnySins.DAL;
using JohnnySins.DAL.Repositories;
using JohnnySins.Entities;
using JohnnySins.Code;
using System.Globalization;

namespace JohnnySins.Admin
{
    public partial class StarProfileDetail : BasePage
    {
        public int[] SelectedPhoto { get; set; }
        private void FindSelectedPhoto()
        {
            var selected = new List<int>();

            foreach (GridViewRow row in gridPhoto.Rows)
            {
                if (row.RowType == DataControlRowType.DataRow)
                {
                    var check = row.Cells[0].FindControl("cbSelectedPhoto") as CheckBox;
                    if (check.Checked)
                    {
                        var id = row.FindControl("hidId") as HiddenField;
                        selected.Add(int.Parse(id.Value));
                    }
                }
            }

            SelectedPhoto = selected.ToArray();
        }

        public int[] Selected { get; set; }
        private void FindSelected()
        {
            var selected = new List<int>();

            foreach (GridViewRow row in grid.Rows)
            {
                if (row.RowType == DataControlRowType.DataRow)
                {
                    var check = row.Cells[0].FindControl("cbSelected") as CheckBox;
                    if (check.Checked)
                    {
                        var id = row.FindControl("hidId") as HiddenField;
                        selected.Add(int.Parse(id.Value));
                    }
                }
            }

            Selected = selected.ToArray();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            FindSelectedPhoto();
            FindSelected();

            if (!IsPostBack)
            {
                LoadProfile();
                LoadProfilePhoto();
                LoadProfileScene();
            }
        }

        protected void LoadProfile()
        {
            using (var repo = RepositoryFactory.ResolveRepository<StarProfileRepository>())
            {
                if (StarProfileId != -1)
                {
                    StarProfile itm = repo.Find(StarProfileId);
                    if (itm == null)
                    {
                        AlertAndClear("Profile not found!");
                        return;
                    }
                    txtFirstName.Text = itm.FirstName;
                    txtLastName.Text = itm.LastName;
                    ddlGender.SelectedValue = itm.Gender;

                    txtEthnicity.Text = itm.Ethnicity;
                    txtCountryOfOrigin.Text = itm.CountryOfOrigin;
                    txtState.Text = itm.State;
                    txtPlaceOfBirth.Text = itm.PlaceOfBirth;
                    txtCity.Text = itm.City;
                    if (itm.DateOfBirth.HasValue)
                        txtDateOfBirth.Text = itm.DateOfBirth.Value.ToString("d", new CultureInfo("en-US"));
                    else
                        txtDateOfBirth.Text = "";
                    txtEyeColor.Text = itm.EyeColor;
                    txtHairColor.Text = itm.HairColor;
                    txtHeight.Text = itm.Height;
                    txtWeight.Text = itm.Weight;
                    txtMyTwitter.Text = itm.MyTwitter;
                    txtWebsiteTwitter.Text = itm.WebsiteTwitter;

                    lblDialogTitle.Text = "Edit Profile";
                }
                else
                {
                    lblDialogTitle.Text = "New Profile";
                    upPhoto.Visible = false;
                }

            }
        }

        protected void LoadProfilePhoto()
        {
            using (var repo = RepositoryFactory.ResolveRepository<StarProfileRepository>())
            {
                gridPhoto.DataSource = repo.FindPhotos(StarProfileId, false);
                gridPhoto.DataBind();
            }
        }

        protected void LoadProfileScene()
        {
            using (var repo = RepositoryFactory.ResolveRepository<StarProfileRepository>())
            {
                grid.DataSource = repo.FindScenes(StarProfileId, false);
                grid.DataBind();
            }
        }

        protected void btnSaveProfile_Click(object sender, EventArgs e)
        {
            using (var repo = RepositoryFactory.ResolveRepository<StarProfileRepository>())
            {
                StarProfile itm;
                if (StarProfileId == -1)
                    itm = new StarProfile();
                else
                    itm = repo.Find(StarProfileId);

                itm.FirstName = txtFirstName.Text;
                itm.LastName = txtLastName.Text;
                itm.Gender = ddlGender.SelectedValue;

                itm.Ethnicity = txtEthnicity.Text;
                itm.CountryOfOrigin = txtCountryOfOrigin.Text;
                itm.State = txtState.Text;
                itm.PlaceOfBirth = txtPlaceOfBirth.Text;
                itm.City = txtCity.Text;
                if (!string.IsNullOrEmpty(txtDateOfBirth.Text))
                    itm.DateOfBirth = DateTime.ParseExact(txtDateOfBirth.Text, "d", new CultureInfo("en-US"));
                else
                    itm.DateOfBirth = null;

                txtEyeColor.Text = itm.EyeColor;
                txtHairColor.Text = itm.HairColor;
                txtHeight.Text = itm.Height;
                txtWeight.Text = itm.Weight;
                txtMyTwitter.Text = itm.MyTwitter;
                txtWebsiteTwitter.Text = itm.WebsiteTwitter;

                if (StarProfileId == -1)
                {
                    repo.Insert(itm);
                    StarProfileId = itm.StarProfileId;
                    lblDialogTitle.Text = "Edit Profile";

                    LoadProfilePhoto();
                    upPhoto.Visible = true;
                    upPhoto.Update();

                    LoadProfileScene();
                    upScene.Visible = true;
                    upScene.Update();
                }
                else
                    repo.Update(itm);
               
                
            }
            ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "Alert", string.Format("alert('{0}');", "Profile saved"), true);
        }

        protected void btnCancelProfile_Click(object sender, EventArgs e)
        {
            LoadProfile();
        }

        protected void gridPhoto_RowDataBound(Object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                StarProfilePhoto item = (StarProfilePhoto)e.Row.DataItem;

                ImageButton ibtn = (ImageButton)e.Row.FindControl("btnDeactivate");
                ibtn.Visible = item.IsActive;

                ibtn = (ImageButton)e.Row.FindControl("btnActivate");
                ibtn.Visible = !item.IsActive;

                CheckBox cbSelected = (CheckBox)e.Row.FindControl("cbSelectedPhoto");
                cbSelected.Checked = SelectedPhoto.Contains(item.StarProfilePhotoId);
            }

            if (e.Row.RowType == DataControlRowType.Header)
            {
                CheckBox chbAll = (CheckBox)e.Row.FindControl("chbAllPhoto");
                chbAll.Checked = (SelectedPhoto.Length == (gridPhoto.DataSource as IEnumerable<StarProfilePhoto>).Count());
            }
        }

        protected void gridPhoto_Command(object sender, CommandEventArgs e)
        {
            int id = Convert.ToInt32(e.CommandArgument.ToString());

            if (e.CommandName == "delete")
            {
                DeletePhoto(id);
            }
            else
            {
                using (var repo = RepositoryFactory.ResolveRepository<StarProfileRepository>())
                {
                    switch (e.CommandName)
                    {
                        case "deactivate":
                            repo.DeactivatePhoto(id);
                            break;
                        case "activate":
                            repo.ActivatePhoto(id);
                            break;
                    }
                }
            }

            LoadProfilePhoto();
        }

        protected void grid_RowDataBound(Object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                StarProfileScene item = (StarProfileScene)e.Row.DataItem;

                ImageButton ibtn = (ImageButton)e.Row.FindControl("btnDeactivate");
                ibtn.Visible = item.IsActive;

                ibtn = (ImageButton)e.Row.FindControl("btnActivate");
                ibtn.Visible = !item.IsActive;

                CheckBox cbSelected = (CheckBox)e.Row.FindControl("cbSelected");
                cbSelected.Checked = Selected.Contains(item.StarProfileSceneId);
            }

            if (e.Row.RowType == DataControlRowType.Header)
            {
                CheckBox chbAll = (CheckBox)e.Row.FindControl("chbAll");
                chbAll.Checked = (Selected.Length == (grid.DataSource as IEnumerable<StarProfileScene>).Count());
            }
        }

        protected void grid_Command(object sender, CommandEventArgs e)
        {
            int id = Convert.ToInt32(e.CommandArgument.ToString());

            if (e.CommandName == "delete")
            {
                DeleteScene(id);
            }
            else
            {
                using (var repo = RepositoryFactory.ResolveRepository<StarProfileRepository>())
                {
                    switch (e.CommandName)
                    {
                        case "deactivate":
                            repo.DeactivateScene(id);
                            break;
                        case "activate":
                            repo.ActivateScene(id);
                            break;
                    }
                }
            }

            LoadProfileScene();
        }

        protected void DeletePhoto(int id)
        {
            using (var repo = RepositoryFactory.ResolveRepository<StarProfileRepository>())
            {
                StarProfilePhoto itm = repo.FindPhoto(id);
                if (itm != null)
                {
                    string path = Path.Combine(StarProfilePhotoPath, itm.PhysFileName);
                    if (File.Exists(path))
                        try
                        {
                            File.Delete(path);
                        }
                        catch (Exception ex) { }
                    repo.DeletePhoto(id);
                }
            }
        }

        protected void DeleteSceneFile(int id, string quality, string os)
        {
            string path = GetSceneFilePath(id, quality, os);
            if (File.Exists(path))
                try
                {
                    File.Delete(path);
                }
                catch (Exception ex) { }
        }

        protected void DeleteScene(int id)
        {
            using (var repo = RepositoryFactory.ResolveRepository<StarProfileRepository>())
            {
                StarProfileScene itm = repo.FindScene(id);
                if (itm != null)
                {
                    DeleteSceneFile(id, "", "");
                    DeleteSceneFile(id, "poster", "");
                    DeleteSceneFile(id, "hd", "win");
                    DeleteSceneFile(id, "hd", "qt");
                    DeleteSceneFile(id, "ld", "win");
                    DeleteSceneFile(id, "ld", "qt");

                    repo.DeleteScene(id);
                }
            }
        }

        protected void btnDeleteSelectedPhoto_Click(object sender, EventArgs e)
        {
            using (var repo = RepositoryFactory.ResolveRepository<StarProfileRepository>())
            {
                foreach (int id in SelectedPhoto)
                {
                    DeletePhoto(id);
                }
            }

            LoadProfilePhoto();
        }

        protected void btnActivateSelectedPhoto_Click(object sender, EventArgs e)
        {
            using (var repo = RepositoryFactory.ResolveRepository<StarProfileRepository>())
            {
                foreach (int id in SelectedPhoto)
                {
                    repo.ActivatePhoto(id);
                }
            }

            LoadProfilePhoto();
        }

        protected void btnDeactivateSelectedPhoto_Click(object sender, EventArgs e)
        {
            using (var repo = RepositoryFactory.ResolveRepository<StarProfileRepository>())
            {
                foreach (int id in SelectedPhoto)
                {
                    repo.DeactivatePhoto(id);
                }
            }

            LoadProfilePhoto();
        }

        protected void btnReverseSelectedPhoto_Click(object sender, EventArgs e)
        {
            var otherScenes = new List<int>();

            using (var repo = RepositoryFactory.ResolveRepository<StarProfileRepository>())
            {
                foreach (var scene in repo.FindPhotos(StarProfileId, false))
                {
                    if (!SelectedPhoto.Contains(scene.StarProfilePhotoId))
                    {
                        otherScenes.Add(scene.StarProfilePhotoId);
                    }
                }
            }

            SelectedPhoto = otherScenes.ToArray();
            LoadProfilePhoto();
        }

        protected void chbAllPhoto_CheckChanged(Object sender, EventArgs e)
        {
            bool chk = (sender as CheckBox).Checked;
            if (chk)
            {
                var other = new List<int>();
                using (var repo = RepositoryFactory.ResolveRepository<StarProfileRepository>())
                {
                    foreach (var scene in repo.FindPhotos(StarProfileId, false))
                        other.Add(scene.StarProfilePhotoId);
                }

                SelectedPhoto = other.ToArray();
            }
            else
                SelectedPhoto = new int[] { };

            LoadProfilePhoto();
        }

        protected void btnDeleteSelectedVideo_Click(object sender, EventArgs e)
        {
            using (var repo = RepositoryFactory.ResolveRepository<StarProfileRepository>())
            {
                foreach (int id in Selected)
                {
                    DeleteScene(id);
                }
            }

            LoadProfileScene();
        }

        protected void btnActivateSelectedVideo_Click(object sender, EventArgs e)
        {
            using (var repo = RepositoryFactory.ResolveRepository<StarProfileRepository>())
            {
                foreach (int id in Selected)
                {
                    repo.ActivateScene(id);
                }
            }

            LoadProfileScene();
        }

        protected void btnDeactivateSelectedVideo_Click(object sender, EventArgs e)
        {
            using (var repo = RepositoryFactory.ResolveRepository<StarProfileRepository>())
            {
                foreach (int id in Selected)
                {
                    repo.DeactivateScene(id);
                }
            }

            LoadProfileScene();
        }

        protected void btnReverseSelectedVideo_Click(object sender, EventArgs e)
        {
            var otherScenes = new List<int>();

            using (var repo = RepositoryFactory.ResolveRepository<StarProfileRepository>())
            {
                foreach (var scene in repo.FindAll())
                {
                    if (!Selected.Contains(scene.StarProfileId))
                    {
                        otherScenes.Add(scene.StarProfileId);
                    }
                }
            }

            Selected = otherScenes.ToArray();
            LoadProfileScene();
        }

        protected void btnCopyToHomeSelectedVideo_Click(object sender, EventArgs e)
        {
        }

        protected void chbAll_CheckChanged(Object sender, EventArgs e)
        {
            bool chk = (sender as CheckBox).Checked;
            if (chk)
            {
                var other = new List<int>();
                using (var repo = RepositoryFactory.ResolveRepository<StarProfileRepository>())
                {
                    foreach (var scene in repo.FindScenes(StarProfileId, false))
                        other.Add(scene.StarProfileSceneId);
                }

                Selected = other.ToArray();
            }
            else
                Selected = new int[] { };

            LoadProfileScene();
        }

        protected void upDetailsPhoto_Load(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(Request["__EVENTTARGET"]))
                if (Request["__EVENTTARGET"] != upDetailsPhoto.ClientID)
                    return;

            if (!string.IsNullOrEmpty(Request["__EVENTARGUMENT"]))
            {
                hdIdPhoto.Text = Request["__EVENTARGUMENT"];

                if (hdIdPhoto.Text != "-1")
                {
                    using (var repo = RepositoryFactory.ResolveRepository<StarProfileRepository>())
                    {
                        lblDialogTitle.Text = "EDIT PROFILE PHOTO";
                        StarProfilePhoto itm = repo.FindPhoto(Convert.ToInt32(hdIdPhoto.Text));
                        txtStarProfilePhotoTitle.Text = itm.StarProfilePhotoTitle;
                        ddlIsActivePhoto.SelectedValue = itm.IsActive ? "1" : "0";

                        fuStarProfilePhotoFileName.Visible = false;
                        lblStarProfilePhotoFileName.Visible = true;
                        lblStarProfilePhotoFileName.Text = itm.StarProfilePhotoFileName;
                    }
                }
                else
                {
                    lblDialogTitle.Text = "ADD NEW PROFILE PHOTO";

                    txtStarProfilePhotoTitle.Text = "";
                    ddlIsActivePhoto.SelectedValue = "1";
                    fuStarProfilePhotoFileName.Visible = true;
                    lblStarProfilePhotoFileName.Visible = false;
                }

                ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "ShowEditPhoto", "$('#editDialogPhoto').dialog('open');", true);
            }
        }

        protected void btnSavePhoto_Click(object sender, EventArgs e)
        {
            if (hdIdPhoto.Text == "-1")
                if (!fuStarProfilePhotoFileName.HasFile)
                    return;

            StarProfilePhoto itm = new StarProfilePhoto();
            itm.StarProfileId = StarProfileId;
            itm.StarProfilePhotoTitle = txtStarProfilePhotoTitle.Text;
            itm.IsActive = ddlIsActivePhoto.SelectedValue == "1";

            using (var repo = RepositoryFactory.ResolveRepository<StarProfileRepository>())
            {
                if (hdIdPhoto.Text == "-1")
                {
                    itm.StarProfilePhotoFileName = fuStarProfilePhotoFileName.FileName;
                    repo.InsertPhoto(itm);

                    string filePath = Path.Combine(StarProfilePhotoPath, itm.PhysFileName);
                    fuStarProfilePhotoFileName.SaveAs(filePath);
                }
                else
                {
                    itm.StarProfilePhotoId = Convert.ToInt32(hdIdPhoto.Text);
                    repo.UpdatePhoto(itm);
                }
            }
            LoadProfilePhoto();
        }

        protected void upDetailsScene_Load(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(Request["__EVENTTARGET"]))
                if (Request["__EVENTTARGET"] != upDetailsScene.ClientID)
                    return;

            if (!string.IsNullOrEmpty(Request["__EVENTARGUMENT"]))
            {
                hdId.Text = Request["__EVENTARGUMENT"];


                fuPreviewFileName.Clear();
                fuHDQTFileName.Clear();
                fuHDWinFileName.Clear();
                fuLDQTFileName.Clear();
                fuLDWinFileName.Clear();
                fuPoster.Style.Clear();
                fuPoster.Style.Clear();

                if (hdId.Text != "-1")
                {
                    using (var repo = RepositoryFactory.ResolveRepository<StarProfileRepository>())
                    {
                        lblDialogTitle.Text = "EDIT SCENE";
                        StarProfileScene itm = repo.FindScene(Convert.ToInt32(hdId.Text));
                        txtStarProfileSceneTitle.Text = itm.StarProfileSceneTitle;
                        ddlIsActive.SelectedValue = itm.IsActive ? "1" : "0";

                        if (!string.IsNullOrEmpty(itm.PreviewFileName))
                        {
                            fuPreviewFileName.FileName = itm.PreviewFileName;
                        }

                        if (!string.IsNullOrEmpty(itm.Poster))
                        {
                            fuPoster.Style.Add("display", "none");
                            pnlPoster.Visible = true;
                            lblPoster.Text = itm.Poster;
                            hdPoster.Value = "0";
                        }
                        else
                        {
                            pnlPoster.Visible = false;
                            hdPoster.Value = "1";
                        }

                        if (!string.IsNullOrEmpty(itm.HDQTFileName))
                        {
                            fuHDQTFileName.FileName = itm.HDQTFileName;
                        }

                        if (!string.IsNullOrEmpty(itm.HDWinFileName))
                        {
                            fuHDWinFileName.FileName = itm.HDWinFileName;
                        }

                        if (!string.IsNullOrEmpty(itm.LDQTFileName))
                        {
                            fuLDQTFileName.FileName = itm.LDQTFileName;
                        }

                        if (!string.IsNullOrEmpty(itm.LDWinFileName))
                        {
                            fuLDWinFileName.FileName = itm.LDWinFileName;
                        }
                    }
                }
                else
                {
                    lblDialogTitle.Text = "ADD NEW SCENE";

                    txtStarProfileSceneTitle.Text = "";
                    ddlIsActive.SelectedValue = "1";
                    
                    fuPoster.Visible = true;
                    pnlPoster.Visible = false;
                }

                ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "ShowEditScene", "$('#editDialog').dialog('open');", true);
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            /*if (hdId.Text == "-1")
                if (!fuPreviewFileName.HasFile)
                    return;*/

            using (var repo = RepositoryFactory.ResolveRepository<StarProfileRepository>())
            {
                if (hdId.Text == "-1")
                {
                    StarProfileScene itm = new StarProfileScene();

                    itm.StarProfileId = StarProfileId;
                    itm.StarProfileSceneTitle = txtStarProfileSceneTitle.Text;
                    itm.IsActive = ddlIsActive.SelectedValue == "1";

                    itm.PreviewFileName = fuPreviewFileName.FileName;
                    itm.HDQTFileName = fuHDQTFileName.FileName;
                    itm.HDWinFileName = fuHDWinFileName.FileName;
                    itm.LDQTFileName = fuLDQTFileName.FileName;
                    itm.LDWinFileName = fuLDWinFileName.FileName;
                    itm.Poster = fuPoster.FileName;

                    repo.InsertScene(itm);

                    if (fuPreviewFileName.HasFile)
                    {
                        string filePath = Path.Combine(StarProfileScenePath, "Preview", itm.PhysFileName);
                        fuPreviewFileName.SaveAs(filePath);
                    }
                    if (fuPoster.HasFile)
                    {
                        string filePath = Path.Combine(StarProfileScenePath, "Poster", itm.PhysPoster);
                        fuPoster.SaveAs(filePath);
                    }
                    if (fuHDQTFileName.HasFile)
                    {
                        string filePath = Path.Combine(StarProfileScenePath, "HDQT", itm.PhysHDQTFileName);
                        fuHDQTFileName.SaveAs(filePath);
                    }
                    if (fuHDWinFileName.HasFile)
                    {
                        string filePath = Path.Combine(StarProfileScenePath, "HDWin", itm.PhysHDWinFileName);
                        fuHDWinFileName.SaveAs(filePath);
                    }
                    if (fuLDQTFileName.HasFile)
                    {
                        string filePath = Path.Combine(StarProfileScenePath, "LDQT", itm.PhysLDQTFileName);
                        fuLDQTFileName.SaveAs(filePath);
                    }
                    if (fuLDWinFileName.HasFile)
                    {
                        string filePath = Path.Combine(StarProfileScenePath, "LDWin", itm.PhysLDWinFileName);
                        fuLDWinFileName.SaveAs(filePath);
                    }
                }
                else
                {
                    StarProfileScene itm = repo.FindScene(Convert.ToInt32(hdId.Text));

                    itm.StarProfileSceneTitle = txtStarProfileSceneTitle.Text;
                    itm.IsActive = ddlIsActive.SelectedValue == "1";

                    if (fuPreviewFileName.IsNewFile)
                    {
                        DeleteSceneFile(itm.StarProfileSceneId, "", "");
                        itm.PreviewFileName = "";
                        if (fuPreviewFileName.HasFile)
                        {
                            itm.PreviewFileName = fuPreviewFileName.FileName;
                            string filePath = Path.Combine(StarProfileScenePath, "Preview", itm.PhysFileName);
                            fuPreviewFileName.SaveAs(filePath);
                        }
                    }
                    if (hdPoster.Value == "1")
                    {
                        DeleteSceneFile(itm.StarProfileSceneId, "poster", "");
                        itm.Poster = "";
                        if (fuPoster.HasFile)
                        {
                            itm.Poster = fuPoster.FileName;
                            string filePath = Path.Combine(StarProfileScenePath, "Poster", itm.PhysPoster);
                            fuPoster.SaveAs(filePath);
                        }
                    }
                    if (fuHDQTFileName.IsNewFile)
                    {
                        DeleteSceneFile(itm.StarProfileSceneId, "hd", "qt");
                        itm.HDQTFileName = "";
                        if (fuHDQTFileName.HasFile)
                        {
                            itm.HDQTFileName = fuHDQTFileName.FileName;
                            string filePath = Path.Combine(StarProfileScenePath, "HDQT", itm.PhysHDQTFileName);
                            fuHDQTFileName.SaveAs(filePath);
                        }
                    }
                    if (fuHDWinFileName.IsNewFile)
                    {
                        DeleteSceneFile(itm.StarProfileSceneId, "hd", "Win");
                        itm.HDWinFileName = "";
                        if (fuHDWinFileName.HasFile)
                        {
                            itm.HDWinFileName = fuHDWinFileName.FileName;
                            string filePath = Path.Combine(StarProfileScenePath, "HDWin", itm.PhysHDWinFileName);
                            fuHDWinFileName.SaveAs(filePath);
                        }
                    }
                    if (fuLDQTFileName.IsNewFile)
                    {
                        DeleteSceneFile(itm.StarProfileSceneId, "LD", "qt");
                        itm.LDQTFileName = "";
                        if (fuLDQTFileName.HasFile)
                        {
                            itm.LDQTFileName = fuLDQTFileName.FileName;
                            string filePath = Path.Combine(StarProfileScenePath, "LDQT", itm.PhysLDQTFileName);
                            fuLDQTFileName.SaveAs(filePath);
                        }
                    }
                    if (fuLDWinFileName.IsNewFile)
                    {
                        DeleteSceneFile(itm.StarProfileSceneId, "LD", "Win");
                        itm.LDWinFileName = "";
                        if (fuLDWinFileName.HasFile)
                        {
                            itm.LDWinFileName = fuLDWinFileName.FileName;
                            string filePath = Path.Combine(StarProfileScenePath, "LDWin", itm.PhysLDWinFileName);
                            fuLDWinFileName.SaveAs(filePath);
                        }
                    }

                    repo.UpdateScene(itm);
                }
            }
            LoadProfileScene();
        }

        private string StarProfilePath
        {
            get
            {
                return Server.MapPath(string.Format("~/Files/StarProfile/{0}/", StarProfileId.ToString()));
            }
        }

        private string StarProfilePhotoPath
        {
            get
            {
                string result = Path.Combine(StarProfilePath, "Photo");
                if (!Directory.Exists(result))
                    Directory.CreateDirectory(result);
                return result;
            }
        }

        private string StarProfileScenePath
        {
            get
            {
                string result = Path.Combine(StarProfilePath, "Scene");
                if (!Directory.Exists(result))
                    Directory.CreateDirectory(result);

                string subPath = "";

                subPath = Path.Combine(result, "HDQT");
                if (!Directory.Exists(subPath))
                    Directory.CreateDirectory(subPath);
                subPath = Path.Combine(result, "HDWin");
                if (!Directory.Exists(subPath))
                    Directory.CreateDirectory(subPath);

                subPath = Path.Combine(result, "LDQT");
                if (!Directory.Exists(subPath))
                    Directory.CreateDirectory(subPath);
                subPath = Path.Combine(result, "LDWin");
                if (!Directory.Exists(subPath))
                    Directory.CreateDirectory(subPath);
                
                subPath = Path.Combine(result, "Poster");
                if (!Directory.Exists(subPath))
                    Directory.CreateDirectory(subPath);
                subPath = Path.Combine(result, "Preview");
                if (!Directory.Exists(subPath))
                    Directory.CreateDirectory(subPath);

                return result;
            }
        }

        protected string GetSceneFilePath(int id, string quality, string os)
        {
            using (var repo = RepositoryFactory.ResolveRepository<StarProfileRepository>())
            {
                StarProfileScene video = repo.FindScene(id);

                string basePath = StarProfileScenePath;

                if ((quality == "") && (os == ""))
                    return Path.Combine(basePath, "Preview", video.PhysFileName);

                if ((quality == "poster") && (os == ""))
                    return Path.Combine(basePath, "Poster", video.PhysPoster);

                if (quality == "hd")
                {
                    if (os == "qt")
                    {
                        return Path.Combine(basePath, "HDQT", video.PhysHDQTFileName);
                    }
                    else
                    {
                        return Path.Combine(basePath, "HDWin", video.PhysHDWinFileName);
                    }
                }
                else
                {
                    if (os == "qt")
                    {
                        return Path.Combine(basePath, "LDQT", video.PhysLDQTFileName);
                    }
                    else
                    {
                        return Path.Combine(basePath, "LDWin", video.PhysLDWinFileName);
                    }
                }
            }
        }

        private int StarProfileId
        {
            get
            {
                if (ViewState["StarProfileId"] == null)
                {
                    int id = -1;

                    if (Request.QueryString["pid"] == null)
                        return -1;

                    if (!string.IsNullOrEmpty(Request.QueryString["pid"].ToString()))
                    {
                        if (int.TryParse(Request.QueryString["pid"].ToString(), out id))
                            return id;
                        else
                            return -1;
                    }
                    else
                        return -1;
                }
                else
                    return Convert.ToInt32(ViewState["StarProfileId"]);
            }
            set
            {
                ViewState["StarProfileId"] = value;
            }
        }
    }
}